package com.app.piemoment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.app.piemoment.api.ApiResult;
import com.app.piemoment.api.ApiReturnResultListener;
import com.app.piemoment.api.UserAPI;
import com.app.piemoment.entity.City;
import com.app.piemoment.entity.CityListener;
import com.app.piemoment.entity.CitySelectDialog;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.entity.PieUser;
import com.app.piemoment.view.PmDialog;

public class ComplateUserInfo3Activity extends Activity {
	protected static final int LOAD_PROGRESS = 1111;
	public int sex = 1;
	EditText intro_EditText, individuality_EditText;
	Button ok_Button, currentCity_EditText, domain_EditText,
			occupation_EditText;
	SharedPreferences userInfo;
	String[] occupationSelect = new String[] { "工作", "学习", "待业" };
	String[] domainSelect = new String[] { "计算机", "医药", "教育" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regedit_info_step3);
		userInfo = getSharedPreferences(BaseActivity.PREF_NAME,
				MODE_WORLD_READABLE);

		currentCity_EditText = (Button) findViewById(R.id.currentCity_EditText);

		currentCity_EditText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				City startCity = new City();
				CitySelectDialog cityDialog = new CitySelectDialog(
						ComplateUserInfo3Activity.this, 0, startCity,
						new CityListener() {

							@Override
							public void onCityResult(City city, int whichOne) {
								currentCity_EditText.setText(city
										.getLastSpace());
							}
						});

			}
		});

		occupation_EditText = (Button) findViewById(R.id.occupation_EditText);
		occupation_EditText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PmDialog.createListView(ComplateUserInfo3Activity.this,
						occupationSelect, 0, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								occupation_EditText
										.setText(occupationSelect[which]);
							}
						});

			}
		});

		domain_EditText = (Button) findViewById(R.id.domain_EditText);

		domain_EditText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PmDialog.createListView(ComplateUserInfo3Activity.this,
						domainSelect, 0, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								domain_EditText.setText(domainSelect[which]);
							}
						});

			}
		});
		intro_EditText = (EditText) findViewById(R.id.intro_EditText);
		individuality_EditText = (EditText) findViewById(R.id.individuality_EditText);
		ok_Button = (Button) findViewById(R.id.ok_button);
		ok_Button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				userInfo.edit().putString(PieUser.occupation_S,
						occupation_EditText.getText().toString()).commit();

				userInfo.edit().putString(PieUser.domain_S,
						domain_EditText.getText().toString()).commit();

				userInfo.edit().putString(PieUser.presentation_S,
						intro_EditText.getText().toString()).commit();

				userInfo.edit().putString(PieUser.current_city_S,
						currentCity_EditText.getText().toString()).commit();

				PieUser.getMyStaticInof().setBirth_place(
						userInfo.getString(PieUser.birth_place_S, ""));
				PieUser.getMyStaticInof().setBirthday(
						userInfo.getString(PieUser.birthday_S, ""));
				PieUser.getMyStaticInof().setCurrent_city(
						userInfo.getString(PieUser.current_city_S, ""));
				PieUser.getMyStaticInof().setDegree(
						userInfo.getString(PieUser.degree_S, ""));
				PieUser.getMyStaticInof().setDomain(
						userInfo.getString(PieUser.domain_S, ""));
				PieUser.getMyStaticInof().setFirst_name(
						userInfo.getString(PieUser.first_name_S, ""));
				PieUser.getMyStaticInof().setHeight(
						userInfo.getString(PieUser.height_S, ""));
				PieUser.getMyStaticInof().setNickname(
						userInfo.getString(PieUser.nickname_S, ""));
				PieUser.getMyStaticInof().setOccupation(
						userInfo.getString(PieUser.occupation_S, ""));
				PieUser.getMyStaticInof().setPresentation(
						userInfo.getString(PieUser.presentation_S, ""));
				PieUser.getMyStaticInof().setSchool(
						userInfo.getString(PieUser.school_S, ""));
				PieUser.getMyStaticInof().setSex(
						userInfo.getString(PieUser.sex_S, ""));
				PieUser.getMyStaticInof().setSurnname(
						userInfo.getString(PieUser.surnname_S, ""));
				PieUser.getMyStaticInof().setWeight(
						userInfo.getString(PieUser.weight_S, ""));

				setRandomQA(PieUser.getMyStaticInof());

			}
		});

	}

	private void setRandomQA(PieUser pieuser) {
		UserAPI api = new UserAPI(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(LOAD_PROGRESS);
				if (ApiResult.RESULT_OK == apiResult.getResultCode()) {
					JSONObject jsonEntity = apiResult.getJsonEntity();
					try {
						if (jsonEntity != null) {
							if (jsonEntity.has("status")) {
								String result = jsonEntity.getString("status");
								if (ApiResult.Success.equals(result)) {
									if (jsonEntity.has("data")) {
										JSONArray array = jsonEntity
												.getJSONArray("data");

									}

								}

							}

						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				Intent intent = new Intent();
				intent.setClass(ComplateUserInfo3Activity.this,
						ComplateUserInfo4Activity.class);
				startActivity(intent);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(LOAD_PROGRESS);

			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(LOAD_PROGRESS);
			}
		});
		api.setPersonInfo(1, this, pieuser, LoginInfo.getInstance().getPuid()
				+ "");
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOAD_PROGRESS:
			PmDialog dialog = new PmDialog(this);
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}
}
